package hn.info.hockeynews.fragments;
import java.util.ArrayList;
import java.util.List;

import hn.info.hockeynews.R;
import hn.info.hockeynews.activities.MainActivity;
import hn.info.hockeynews.helpers.DBHelperMatches;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class Fragment1 extends Fragment {
/**
* (non-Javadoc)
*
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
* android.view.ViewGroup, android.os.Bundle)
*/
	/**
	 * with this class you can choose wether you like the ticker entries of one team or all teams
	 * 
	 * */
	private Button save;
	private Button load;
	private Button cancel;
	private CheckBox one;
	private CheckBox all;
	private Spinner spin;
	private DBHelperMatches dbm;
	private SharedPreferences prefs;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return (LinearLayout) inflater.inflate(R.layout.fragment1_layout, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		dbm = new DBHelperMatches();
		//init Layout Objects
		spin = (Spinner) getView().findViewById(R.id.spinner1);
		save = (Button) getView().findViewById(R.id.save);
		load = (Button) getView().findViewById(R.id.load);
		cancel = (Button) getView().findViewById(R.id.cancel);
		one = (CheckBox) getView().findViewById(R.id.one);
		all = (CheckBox) getView().findViewById(R.id.all);
			
		if(prefs == null) {
			one.setChecked(false);
			all.setChecked(true);
		}
		//List<String> spinList = dbm.getTeamNames();
		List<String> spinList = new ArrayList<String>();
		spinList.add("99ers");
		spinList.add("Capitals");
		spinList.add("Dornbirn");
		spinList.add("Innsbruck");
		spinList.add("Fehervar");
		spinList.add("KAC");
		spinList.add("Laibach");
		spinList.add("Linz");
		spinList.add("Salzburg");
		spinList.add("VSV");
		spinList.add("Znojmo");
		spinList.add("Zagreb");
			
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, spinList);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 		
		save.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				saveSettings(spin);
				saveSettings(one);
				saveSettings(all);
			}});
		load.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				loadSettings(spin);
				loadSettings(one);
				loadSettings(all);
			}});
		cancel.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
	    	    intent=new Intent(getActivity(), MainActivity.class);
	    	    startActivity(intent);
			}});
			
		spin.setAdapter(adapter);
			
		one.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick(View v) {
				all.setChecked(false);
				spin.setClickable(true);
			}
		});
		
		all.setOnClickListener( new OnClickListener() {		
			@Override
			public void onClick(View v) {
				one.setChecked(false);
				spin.setClickable(false);	
			}
		});
	}
	
	public void saveSettings(View v) {
		Toast t = Toast.makeText(getActivity(), "save", Toast.LENGTH_LONG);
    	t.show();
    	
    	prefs = getActivity().getSharedPreferences("setting", 0);
    	
    	Editor prefOne = prefs.edit();
    	if(one.isChecked() == true) {
        	prefOne.putBoolean("one", true);
    	}
    	if(one.isChecked() == false) {
        	prefOne.putBoolean("one", false);
    	}
    	prefOne.commit();
    	
    	Editor prefAll = prefs.edit();
    	if(all.isChecked() == true) {
        	prefAll.putBoolean("all", true);
    	}
    	if(all.isChecked() == false) {
        	prefAll.putBoolean("all", false);
    	}
    	prefAll.commit();
    	
    	Editor prefSpin = prefs.edit();
    	prefSpin.putLong("spin", spin.getSelectedItemId());
    	prefSpin.putString("spinname", "99ers");
    	prefSpin.commit();
	}
	
	public void loadSettings(View v) {
		Toast.makeText(getActivity(), "load", Toast.LENGTH_LONG).show();

    	prefs = getActivity().getSharedPreferences("setting", 0);
    	
    	int spiner = Integer.valueOf((int) prefs.getLong("spin", 0));
    	Boolean bOne = prefs.getBoolean("one", false);
    	Boolean bAll = prefs.getBoolean("all", true);
    	spin.setSelection(spiner);
    	one.setChecked(bOne);
    	all.setChecked(bAll);
	}
}